package org.ytor.core.sysapi.dict.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.ytor.common.anno.Dict;
import org.ytor.common.exception.BaseException;
import org.ytor.common.model.R;
import org.ytor.common.util.Strs;
import org.ytor.core.BaseController;
import org.ytor.core.sqlflow.Page;
import org.ytor.core.sysapi.dict.logic.SysDictLogic;
import org.ytor.core.sysapi.dict.model.SysDict;
import org.ytor.core.sysapi.dict.repository.SysDictRepository;
import org.ytor.core.sysapi.dict.req.SysDictItemReq;
import org.ytor.core.sysapi.dict.req.SysDictReq;
import org.ytor.core.sysapi.dict.resp.SysDictItemResp;
import org.ytor.core.sysapi.dict.resp.SysDictResp;
import org.ytor.core.sysapi.dict.service.IDictParse;
import org.ytor.core.model.Person;

import java.util.List;

/**
 * created by yangtong on 2025/4/8 16:45:39
 * <br/>
 */
@RestController
@RequestMapping("/sys/dict")
@Tag(name = "字典")
@RequiredArgsConstructor
public class SysDictController extends BaseController<SysDict, SysDictLogic, SysDictRepository> {

    private final IDictParse dictParse;

    /**
     * 分页查询字典
     */
    @GetMapping("pageDict")
    @Operation(summary = "分页查询字典", description = "分页查询字典")
    public R<Page<SysDictResp>> pageDict(@ParameterObject SysDictReq dictReq) {
        return R.success(logic.pageDict(dictReq));
    }

    /**
     * 新增或编辑字典
     */
    @PostMapping("saveOrUpdateDict")
    @Operation(summary = "新增或编辑字典", description = "新增或编辑字典")
    public R<String> saveOrUpdateDict(@RequestBody SysDictReq dictReq) {
        logic.saveOrUpdateDict(dictReq);
        return R.success("操作成功");
    }

    /**
     * 根据id删除字典
     */
    @DeleteMapping("deleteDict/{ids}")
    @Operation(summary = "根据id删除字典", description = "根据id删除字典")
    public R<String> deleteDict(@PathVariable("ids") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("ids不能为空");
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            logic.deleteDict(id);
        }
        return R.success("删除成功");
    }

    /**
     * 分页查询字典
     */
    @GetMapping("listDictItem")
    @Operation(summary = "查询字典项", description = "查询字典项")
    public R<List<SysDictItemResp>> listDictItem(@ParameterObject SysDictItemReq dictItemReq) {
        return R.success(logic.listDictItem(dictItemReq));
    }

    /**
     * 新增或编辑字典项
     */
    @PostMapping("saveOrUpdateDictItem")
    @Operation(summary = "新增或编辑字典", description = "新增或编辑字典")
    public R<String> saveOrUpdateDictItem(@RequestBody SysDictItemReq dictItemReq) {
        logic.saveOrUpdateDictItem(dictItemReq);
        return R.success("操作成功");
    }

    /**
     * 根据id删除字典项
     */
    @DeleteMapping("deleteDictItem/{ids}")
    @Operation(summary = "根据id删除字典项", description = "根据id删除字典项")
    public R<String> deleteDictItem(@PathVariable("ids") String ids) {
        if (Strs.isEmpty(ids)) {
            throw new BaseException("ids不能为空");
        }
        String[] idList = ids.split(",");
        for (String id : idList) {
            logic.deleteDictItem(id);
        }
        return R.success("删除成功");
    }

    @GetMapping("parseDictTest")
    @Operation(summary = "字典解析测试", description = "字典解析测试")
    @Dict
    public List<Person> parseDictTest() {
        Person person1 = new Person().setName("admin");
        Person person2 = new Person().setName("trest");
        return List.of(person1, person2);
    }

}
